﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Business
{
    public class FinderBiz
    {
        /// <summary>
        /// 价格查询
        /// </summary>
        /// <param name="code"></param>
        /// <param name="prodate"></param>
        /// <returns></returns>
        public static Tuple<bool, string, FinderDataResp> Query(string code, string prodate)
        {
            string query_url = ConfigHelper.AppSetting<string>("query_url");
            string query_key = ConfigHelper.AppSetting<string>("query_key");

            var data = new
            {
                key = query_key,
                code = code,
                proDate = prodate
            };

            string result = HttpHelper.HttpPost(query_url, JsonHelper.Serialize(data));
            if (!string.IsNullOrEmpty(result))
            {
                FinderResponse resp = JsonHelper.Deserialize<FinderResponse>(result);
                if (resp.result_code == 0)
                    return new Tuple<bool, string, FinderDataResp>(true, "", resp.result);
                else
                    return new Tuple<bool, string, FinderDataResp>(false, resp.msg, null);
            }
            else
                return new Tuple<bool, string, FinderDataResp>(false, "价格查询数据为空", null);
        }

        /// <summary>
        /// 料号模糊搜索
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Tuple<bool, string, List<QueryCodeResp>> QueryCode(string code)
        {
            string query_url = ConfigHelper.AppSetting<string>("query_code_url");
            string data = string.Format("size={0}&q=code:{1}", 10, code);

            string result = HttpHelper.HttpGet(query_url, data);
            if (!string.IsNullOrEmpty(result))
            {
                List<QueryCodeResp> list = new List<QueryCodeResp>();
                QueryCodeResult resp = JsonHelper.Deserialize<QueryCodeResult>(result);
                if (resp.hits != null && resp.hits.hits.Count != 0)
                {
                    foreach (var item in resp.hits.hits)
                    {
                        list.Add(item._source);
                    }
                    return new Tuple<bool, string, List<QueryCodeResp>>(true, "", list);
                }
                else
                    return new Tuple<bool, string, List<QueryCodeResp>>(false, "料号模糊查询数据为空", null);
            }
            else
                return new Tuple<bool, string, List<QueryCodeResp>>(false, "料号模糊查询数据为空", null);
        }
    }

    #region 价格查询
    public class FinderResponse
    {
        public int result_code { get; set; }

        public string msg { get; set; }

        public FinderDataResp result { get; set; }
    }

    public class FinderDataResp
    {
        /// <summary>
        /// 产地类型：国产主动件/进口主动件/被动件
        /// </summary>
        public string protype { get; set; }
        /// <summary>
        /// 原提供料号
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 厂商
        /// </summary>
        public string manufacturer { get; set; }
        /// <summary>
        /// 建议价格
        /// </summary>
        public decimal rec_price { get; set; }
        /// <summary>
        /// 货币单位
        /// </summary>
        public string currency { get; set; }
        /// <summary>
        /// 建议价格说明
        /// </summary>
        public string rec_desc { get; set; }
        /// <summary>
        /// 折扣数
        /// </summary>
        public float discount { get; set; }
    }
    #endregion

    #region 料号模糊搜索
    public class QueryCodeResult
    {
        public HitsResp hits { get; set; }
    }
    public class HitsResp
    {
        public List<CodeSourceResp> hits { get; set; }
    }

    public class CodeSourceResp
    {
        public QueryCodeResp _source { get; set; }
    }
    public class QueryCodeResp
    {
        public string code { get; set; }
    }
    #endregion
}
